/***********************************************************************
 * Copyright @ 2011 Zhejiang Yutian Technology Co.,Ltd
 * ==================================================================
 * Project:        LaYouNet
 * FileName:       NetworkInfo.java
 * Describtion:    文件描述
 * Author:         xufei
 * Create Date:    2014-8-16 下午2:34:49
 *********************************************************************/
package net.hzlj.platform.enterprise.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.Locale;

/**
 * @ClassName:Network
 * @Description: 网络状态
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2016-01-08 16:21:00
 */
public class NetworkUtils {

    public static final String NETWORK_CHANGED = "net.hzlj.platform.enterprise.network_changed";

    /**
     * 当前设备是否已连接到网络
     * @param context
     * @return
     */
    public static boolean isConnected(Context context) {
        if (context != null) {
            ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo != null) {
                return networkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 检查网络类型
     * @param context
     * @return
     */
    public static int checkNetworkType(Context context) {
        if (context != null) {
            ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo == null) {
                return Type.DISABLED;
            }
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return Type.WIFI;
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                /**
                 * NETWORK_TYPE_CDMA 网络类型为CDMA
                 * NETWORK_TYPE_EDGE 网络类型为EDGE
                 * NETWORK_TYPE_EVDO_0 网络类型为EVDO0
                 * NETWORK_TYPE_EVDO_A 网络类型为EVDOA
                 * NETWORK_TYPE_GPRS 网络类型为GPRS
                 * NETWORK_TYPE_HSDPA 网络类型为HSDPA
                 * NETWORK_TYPE_HSPA 网络类型为HSPA
                 * NETWORK_TYPE_HSUPA 网络类型为HSUPA
                 * NETWORK_TYPE_UMTS 网络类型为UMTS
                 */
                int subType = networkInfo.getSubtype();
                Log.i("Netwrok", "-----subType----" + subType);
                if (subType == TelephonyManager.NETWORK_TYPE_GPRS) {
                    String apn = networkInfo.getExtraInfo().toLowerCase(Locale.getDefault());
                    if ("cmnet".equals(apn)) {
                        return Type.CMNET;
                    } else if ("cmwap".equals(apn)) {
                        return Type.CMWAP;
                    }
                } else if (subType == TelephonyManager.NETWORK_TYPE_EDGE) {
                    return Type.EDGE;
                } else if (subType == TelephonyManager.NETWORK_TYPE_CDMA) {
                    return Type.CDMA;
                } else if (subType == TelephonyManager.NETWORK_TYPE_UMTS) {
                    return Type.UMTS;
                } else if (subType == TelephonyManager.NETWORK_TYPE_EVDO_0) {
                    return Type.EVDO_0;
                } else if (subType == TelephonyManager.NETWORK_TYPE_EVDO_A) {
                    return Type.EVDO_A;
                } else if (subType == TelephonyManager.NETWORK_TYPE_HSPAP) {
                    return Type.HSDPA;
                } else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {
                    return Type.LTE;
                } else if (subType == TelephonyManager.NETWORK_TYPE_HSPA) {
                    return Type.HSPA;
                }
            }
        }
        return Type.UNKNOWN;
    }

    public static class Type {
        /**
         * NETWORK_TYPE_CDMA 网络类型为CDMA
         * NETWORK_TYPE_EDGE 网络类型为EDGE
         * NETWORK_TYPE_EVDO_0 网络类型为EVDO0
         * NETWORK_TYPE_EVDO_A 网络类型为EVDOA
         * NETWORK_TYPE_GPRS 网络类型为GPRS
         * NETWORK_TYPE_HSDPA 网络类型为HSDPA
         * NETWORK_TYPE_HSPA 网络类型为HSPA
         * NETWORK_TYPE_HSUPA 网络类型为HSUPA
         * NETWORK_TYPE_UMTS 网络类型为UMTS
         */

        /** wifi*/
        public final static int WIFI = 0;
        /** 2g-cmwap*/
        public final static int CMWAP = 1;
        /** 2g-cmnet*/
        public final static int CMNET = 2;
        /** 2.75g*/
        public final static int EDGE = 3;
        /** 3g-cdma*/
        public final static int CDMA = 4;
        /** 3g*/
        public final static int UMTS = 5;
        /** unknown*/
        public final static int UNKNOWN = 6;
        /** 无网络*/
        public final static int DISABLED = 7;

        public final static int EVDO_0 = 8;

        public final static int EVDO_A = 9;

        public final static int HSDPA = 10;

        public final static int HSPA = 11;

        public final static int LTE = 12;

    }

}
